/**
 * 
 */
package com.huawei.struts.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.huawei.struts.form.RoleForm;
import com.huawei.struts.service.ICommonService;
import com.huawei.struts.service.IRoleService;
import com.huawei.struts.util.PageBean;
import com.st.model.LjwPremissionT;
import com.st.model.LjwRoleT;

/**
 * @author huganpu
 *
 */
public class RoleAction extends BaseAction {

	private static Logger logger=Logger.getLogger(RoleAction.class);
	
	private final String returnUrl="/role.do?method=init";
	
	private IRoleService roleService;
	private ICommonService commonService;
	 
	public void setCommonService(ICommonService commonService) {
		this.commonService = commonService;
	}

	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}

	/**
	 * 初始化用户列表
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public ActionForward init(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoleForm roleForm =(RoleForm)form;
		logger.debug("into role init method ...");
		roleForm.setRole(new LjwRoleT());//清空所有条件
		PageBean pageBean=roleService.getRoleList(roleForm.getRole(), roleForm.getPageBean());
		request.setAttribute("pageBean", pageBean);
		return mapping.findForward("toList");
	}
	
	public ActionForward search(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoleForm roleForm =(RoleForm)form;
		logger.debug("into role search method ...");
		PageBean pageBean=roleService.getRoleList(roleForm.getRole(), roleForm.getPageBean());
		request.setAttribute("pageBean", pageBean);
		return mapping.findForward("toList");
	}
	
	/**
	 * 跳转到用户创建页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toCreateRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoleForm roleForm =(RoleForm)form;
		logger.debug("into role toCreateRole method ...");
		this.saveToken(request);
		return mapping.findForward("toAdd");
	}
	
	/**
	 * 创建用户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward createRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoleForm roleForm =(RoleForm)form;
		logger.debug("into role createRole method ...");
		if(this.isTokenValid(request,true))
		{
			roleService.addRole(roleForm.getRole());
			return super.goSuccess("操作成功", returnUrl, request, response, mapping);
		}
		else
		{
			return super.goFail("表单不能重复提交", returnUrl, request, response,mapping);
		}
	}
	
	/**
	 * 跳转到用户修改页面
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward toUpdateRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoleForm roleForm =(RoleForm)form;
		logger.debug("into role toUpdateRole method ...");
		Integer id=new Integer(request.getParameter("id"));
		roleForm.setRole(roleService.getRoleInfoByid(id));
		this.saveToken(request);
		return mapping.findForward("toUpdate");
	}
	
	/**
	 * 修改用户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward updateRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoleForm roleForm =(RoleForm)form;
		logger.debug("into role updateRole method ...");
		if(this.isTokenValid(request,true))
		{
			roleService.modifyRole(roleForm.getRole());
			return super.goSuccess("操作成功", returnUrl, request, response, mapping);
		}
		else
		{
			return super.goFail("表单不能重复提交", returnUrl, request, response,mapping);
		}
	}
	
	public ActionForward delRole(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoleForm roleForm =(RoleForm)form;
		Integer id=new Integer(request.getParameter("id"));
		roleForm.getRole().setRoleId(id);
		roleService.delRole(roleForm.getRole());
		return super.goSuccess("操作成功", returnUrl, request, response, mapping); 
	}

	public ActionForward toRoleGrant(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoleForm roleForm =(RoleForm)form;
		Integer id=new Integer(request.getParameter("id"));
		roleForm.getRole().setRoleId(id);
		LjwRoleT role=roleService.getRoleInfoByid(id);
		List<LjwPremissionT> listPremission=commonService.findAllPremission(id);
		request.setAttribute("role", role);
		request.setAttribute("listPremission", listPremission);
		return mapping.findForward("toRoleGrant"); 
	}
	
	public ActionForward roleGrant(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RoleForm roleForm =(RoleForm)form;
		
		String[] permissionIds=request.getParameterValues("permissionId");
		commonService.deleteAclentry(roleForm.getRole().getRoleId());
		commonService.saveRoleGrant(permissionIds,roleForm.getRole().getRoleId());
		return super.goSuccess("操作成功", returnUrl, request, response, mapping); 
	}
	
}
